//$(document).ready(function () {
//    debugger;
//    var path = window.location.pathname;
//    if (path == "/") {
//        //  window.location.href = "../en";
//        window.location.pathname = "/en";
//    }
//});
function searchProgram(query, exact) {
    if (query == '?????? ?????????...') {
        alert("Nothing to search");
        return;
    }
    else {
        var lng = $("#langlbl").val();
        /*Sending request to controller and getting JSON data to construct popup page and show results*/
        query = query.substring(0, 60);
        var query_64 = Base64.encode(query);
        //Replace('-', '+').Replace('_', '/')
        query_64 = query_64.replace(/\+/g, "-").replace(/\//g,"_");
        var url = "/"+lng+"/ShowResults/" + query_64 + "/" + exact;
        window.location = url;
    }
}

function searchNotExact () {
   if ($("#txtSearchProgram").val() != "") {
      searchProgram($("#txtSearchProgram").val(), 0);
   }
}

function bodyOnloadCallbacks() {
    //register all nessesary functions to be executed after body load
    scrollToHilighted();
}

function scrollToHilighted() {
    var offset = $(".Home-page_Prog_onShare").offset();
    if (offset) {
        $('html, body').animate({
            scrollTop: offset.top - 100
        }, 1500, function () {
            $('.MainMaster-Page_Main_Div').animate({
                scrollTop: offset.top - 100
            });
        });
    }
}

window.onscroll = function (e) {
    var obj = document.getElementById("home_prog_day_chooser");
    var catObj = document.getElementById('catchooser');
    if (!obj)
        return;

    if (window.pageYOffset > 128) {
        var obj = document.getElementById("home_prog_day_chooser");
        obj.style.width = "580px";
        obj.style.height = "30px";
        obj.style.position = "fixed";
        obj.style.top = "0";
        obj.style.marginTop = "0";
        obj.style.marginLeft = "6px";
        obj.style.zIndex = "3";
        //obj.style.borderRadius = "0 0 7px 7px";
        obj.style.borderBottom = "3px solid #0C64C0";
        obj.style.height = "30px";
        catObj.style.marginTop = "48px";
    }
    else {
        // obj.removeAttribute('style') doesn't work on google chrome
        obj.style.width = "";
        obj.style.height = "";
        obj.style.position = "";
        obj.style.top = "";
        obj.style.marginTop = "";
        obj.style.marginLeft = "";
        obj.style.zIndex = "";
        //obj.style.borderRadius = "0 0 7px 7px";
        obj.style.borderBottom = "";
        obj.style.height = "";
        catObj.style.marginTop = "0";
    }
};

function keepDayChooserToTop()
{
    

}

// Converting to base64
var Base64 = {

    // private property
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode: function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // private method for UTF-8 encoding
    _utf8_encode: function (string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    }

}

var Analytics = {
    event: function (category, action, label, value) {
        var obj = {
            hitType: 'event',
            eventCategory: category,
            eventAction: action,
            eventLabel: label || document.URL,
            eventValue: value || 0
        };

        ga('send', obj);
    }
};

var disableLikeBoxCookie = 'disableslikebox';

function floatingBox() {
    var shownFlag = false;

    var likeBox = $('.js-fixed-like-box');
    
    if (!likeBox.length || !($('.Home-page_Prog_Div_Programs').length || $('.Home-page_Channel_Div_Video').length)) {
        return;
    }

    $('#js-fixed-like-box-close,#js-fixed-like-box-hide').on('click', function (e) {
        e.preventDefault();

        if ($(this).data('ga-category') && $(this).data('ga-action')) {
            Analytics.event($(this).data('ga-category'), $(this).data('ga-action'));
        }

        $.cookie(disableLikeBoxCookie, '1', {
            expires: 14,
            path: '/'
        });
        likeBox.hide();
    });

    if ($.cookie(disableLikeBoxCookie) === null || $.cookie(disableLikeBoxCookie) === undefined) {
        likeBox.css('display', 'block');

        $(document).scroll(function () {
            if ($('body').hasClass('mobile')) {
                var scrollTopDistance = $('.Home-page_Channel_Div').height() - 200;
            } else {
                if ($('.Home-page_Prog_Div_Programs').length > 0) {
                    var scrollTopDistance = $('.Home-page_Prog_Div_Programs').height() - 1000;
                    scrollTopDistance = scrollTopDistance < 0 ? 0 : scrollTopDistance;
                }
                else if ($('.Home-page_Channel_Div_Video').length > 0) {
                    var scrollTopDistance = $('.Home-page_Channel_Div_Video').height() - 800;
                    scrollTopDistance = scrollTopDistance < 0 ? 0 : scrollTopDistance;
                }
            }

            // Do not show if modal popup is open
            if ($("#FBLikePopupModal").dialog("isOpen")) {
                likeBox.stop(true).animate({
                    right: '-362px'
                }, 300);

                return;
            }

            if ($(document).scrollTop() > scrollTopDistance) {
                if ($('body').hasClass('mobile')) {
                    likeBox.animate({
                        right: '-5px'
                    }, 500);
                } else {
                    likeBox.animate({
                        right: '15px'
                    }, 500);
                }

                if (!shownFlag) {
                    Analytics.event(likeBox.data('ga-category'), likeBox.data('ga-action'));
                    shownFlag = true;
                }

            } else {
                likeBox.stop(true).animate({
                    right: '-362px'
                }, 300);
            }
        });
    }

    var socialOverlayDialog = $('#js-dialog-social-overlay');
    socialOverlayDialog.bind('dialog-open', function () {
        likeBox.hide();
    });
    socialOverlayDialog.bind('dialog-close', function () {
        likeBox.show();
    });
}

function FBPopupModal(selection) {
    var referrer = document.referrer;
    if (referrer.indexOf("tvcuyc.am") < 0) {
        return;
    }

    if ($.cookie(disableLikeBoxCookie) === null || $.cookie(disableLikeBoxCookie) === undefined) {
        setTimeout(function () {
            $("#FBLikePopupModal").dialog("open");

            $("#FBModalClose").on("click", function () {
                $("#FBLikePopupModal").dialog("close");
                $.cookie(disableLikeBoxCookie, '1', {
                    expires: 14,
                    path: '/'
                });
            });
        }, 20000);
    }

}